///Users/joyban/Desktop/citydown/citystar/cityStar.xcodeproj
//  CommodityListViewController.m
//  cityStar
//
//  Created by joyban on 14-12-23.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "CommodityListViewController.h"
#import "CommodityListCell.h"
#import "SingleTableView.h"
#import "SVPullToRefresh.h"
#import "GetSupplierQuery.h"
#import "HTDefine.h"
#import "HWSearchViewController.h"
#import "WaresDetailViewController.h"

NSString *const ComodityListTableViewCellID = @"ComodityListCellID";

@interface CommodityListViewController ()<SingleTableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *CommodityListTableView;

@property (weak, nonatomic) IBOutlet UIButton *btnAlltypes;
@property (weak, nonatomic) IBOutlet UIButton *btnIntelligence;
@property (nonatomic, strong) NSMutableArray *arrayRequest;

@property (nonatomic, weak) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (strong, nonatomic) SingleTableView *typeSingleTableView;

@property (nonatomic, weak) IBOutlet UIImageView *noopsycheImageView;
@property (strong, nonatomic) SingleTableView *noopsycheSingleTableView;

@property (nonatomic, assign) BOOL isshowHeaderView;
@property (nonatomic, assign) int page;


@end

@implementation CommodityListViewController

-(id)initWithtypeOfShowSearchHeaderView:(BOOL)isShow
{
    self = [super init];
    if (self) {
        self.isshowHeaderView = isShow;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品列表";
    [self addBackButton];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithImage:[UIImage imageNamed:@"bsearch" ] actionBlock:^{
        HWSearchViewController *hwSearchViewController = [[HWSearchViewController alloc]init];
        hwSearchViewController.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:hwSearchViewController animated:YES];
    }];
    self.arrayRequest = [[NSMutableArray alloc]init];

    [self.CommodityListTableView addPullToRefreshWithActionHandler:^{
        self.page = 1;
        [weakSelf webRequest_GetShoppingMore:NO];
    }];
    [self.CommodityListTableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetShoppingMore:YES];
    }];
    [self.CommodityListTableView triggerPullToRefresh];

    [self setUpTableViews];
}

-(void)webRequest_GetShoppingMore:(BOOL)isMore
{
    NSString *postUrl = [NSString stringWithFormat:@"%@supplier/%@/page/%i/city/%@",KInterface_GetGoodsQuery,self.shopid,isMore?self.page:0,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]?[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]:@"122"];
//    NSString *postUrl = [NSString stringWithFormat:@"%@supplier/%@",KInterface_GetGoodsQuery,self.shopid];

    [HttpRequestManager getMethod:[postUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            if (isMore) {
                [self.arrayRequest addObjectsFromArray:response[@"value"][@"content"]];
                self.page = self.page + 1;
            }else{
                self.arrayRequest = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
            }
            [self.CommodityListTableView reloadData];
//            if (self.arrayRequest.count == 0) {
//                self.nothinglabel.hidden = NO;
//            }else{
//                self.nothinglabel.hidden = YES;
//            }
        }
        if (isMore) {
            [self.CommodityListTableView.infiniteScrollingView stopAnimating];
        }else{
            [self.CommodityListTableView.pullToRefreshView stopAnimating];
        }
    }];

}

- (void)addRightButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 20, 20)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    if (self.isshowHeaderView) {
//        self.searchgoodsBtn.selected = YES;
//        self.CommodityListTableView.tableHeaderView = self.headerView;
    }else{
        self.CommodityListTableView.tableHeaderView = nil;
    }
    [self.CommodityListTableView registerNib:[CommodityListCell nib] forCellReuseIdentifier:ComodityListTableViewCellID];
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CommodityListCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayRequest.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GetGoodsQuery *commodity = [GetGoodsQuery objectFromDictionary:self.arrayRequest[indexPath.row]];
    WaresDetailViewController *waresDetailViewController = [[WaresDetailViewController alloc]initWithCommodityID:commodity.objectId];
    [self.navigationController pushViewController:waresDetailViewController animated:YES];

    [tableView reloadData];
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommodityListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:ComodityListTableViewCellID];
    GetGoodsQuery *commodity = [GetGoodsQuery objectFromDictionary:self.arrayRequest[indexPath.row]];
    ListdetailCell.contentLabel.text = commodity.name;
    [ListdetailCell configWithCommidity:commodity];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return ListdetailCell;
}

//#pragma mark - Actions
//-(IBAction)allTypes_atction:(id)sender
//{
//    if(self.btnAlltypes.selected == NO)
//    {
//        self.btnAlltypes.selected = YES;
//    }else{
//        self.btnAlltypes.selected = NO;
//    }
//    self.btnIntelligence.selected = NO;
//    
//    [self.typeSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
//    self.typeSingleTableView.dataArray = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"];
//}
//
//-(IBAction)Intelligence_atction:(id)sender
//{
//    if(self.btnIntelligence.selected == NO)
//    {
//        self.btnIntelligence.selected = YES;
//    }else{
//        self.btnIntelligence.selected = NO;
//    }
//    self.btnAlltypes.selected = NO;
//    
//    [self.noopsycheSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
//    self.noopsycheSingleTableView.dataArray =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"];
//}
//
//#pragma mark - singleTableViewDelegate
//- (SingleTableView *)typeSingleTableView
//{
//    if (!_typeSingleTableView) {
//        _typeSingleTableView = [[SingleTableView alloc] init];
//        _typeSingleTableView.delegate = self;
//        [self addChildViewController:_typeSingleTableView];
//        [self.view addSubview:_typeSingleTableView.view];
//        [self.view bringSubviewToFront:self.topView];
//        _typeSingleTableView.arrawImageView = self.typeImageView;
//    }
//    return _typeSingleTableView;
//}
//
//- (void)singleTableView:(SingleTableView *)tableView didSelectedCellAtIndexPath:(NSIndexPath *)indexPath
//{
//    self.btnIntelligence.selected = NO;
//    self.btnAlltypes.selected = NO;
//    if (tableView == self.typeSingleTableView) {
//        NSString *celltest = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"][indexPath.row];
//        if (celltest.length > 4) {
//            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 4)]];
//            [self.btnAlltypes setTitle:btnTitle forState:UIControlStateNormal];
//        }else{
//            [self.btnAlltypes setTitle:celltest forState:UIControlStateNormal];
//            
//        }
//    }else
//    {
//        NSString *celltest =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"][indexPath.row];
//        if (celltest.length > 5) {
//            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 5)]];
//            [self.btnIntelligence setTitle:btnTitle forState:UIControlStateNormal];
//        }else{
//            [self.btnIntelligence setTitle:celltest forState:UIControlStateNormal];
//            
//        }
//    }
//    
//}
//
//- (SingleTableView *)noopsycheSingleTableView
//{
//    if (!_noopsycheSingleTableView) {
//        _noopsycheSingleTableView = [[SingleTableView alloc] init];
//        _noopsycheSingleTableView.delegate = self;
//        [self addChildViewController:_noopsycheSingleTableView];
//        [self.view addSubview:_noopsycheSingleTableView.view];
//        [self.view bringSubviewToFront:self.topView];
//        _noopsycheSingleTableView.arrawImageView = self.noopsycheImageView;
//    }
//    return _noopsycheSingleTableView;
//}
//-(void)cancleTabelView
//{
//    self.btnIntelligence.selected = NO;
//    self.btnAlltypes.selected = NO;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
